__author__ = 'Pierre Moermans'

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from datetime import date

class TimesheetPDFCreator:
    width,height = A4

    def __init__(self, output, holidays=[]):
        self.canvas = canvas.Canvas(output, pagesize=A4)
        self.week_added = 0;
        self.holidays = holidays

    def setHeader(self, consultant, period , project, manager):
        timesheet_line_y = self.height - 40
        period_line_y = timesheet_line_y - 30
        line_height = 25
        self.canvas.setFont("Helvetica", 20)
        self.canvas.drawRightString(self.width / 2,timesheet_line_y, "Timesheet:")
        self.canvas.drawString(self.width / 2 + 3,timesheet_line_y, consultant)
        self.canvas.setFont("Helvetica", 15)
        self.canvas.drawRightString(self.width / 2,period_line_y, "Period:")
        self.canvas.drawString(self.width / 2 + 3,period_line_y, period)
        self.canvas.drawRightString(self.width / 2,period_line_y - line_height, "Project:")
        self.canvas.drawString(self.width / 2 + 3,period_line_y - line_height, project)
        self.canvas.drawRightString(self.width / 2,period_line_y - 2 * line_height, "Manager:")
        self.canvas.drawString(self.width / 2 + 3,period_line_y - 2 * line_height, manager)

    def addWeek(self, weekNumber, days):
        side_margin = 40
        week_height = 51
        week_space = 20
        week_y = self.height - 250 - self.week_added * (week_space + week_height);
        day_col_width = 50
        first_col_width = self.width - 2 * side_margin - 7 * day_col_width


        self.canvas.setFillColorRGB(0,0,0)
        self.canvas.rect(side_margin, week_y, first_col_width, week_height)
        self.canvas.setFont("Helvetica", 13)
        self.canvas.drawString(side_margin + 3, week_y + 35, "Week " + str(weekNumber))
        self.canvas.setFont("Helvetica", 9)
        self.canvas.drawRightString(side_margin + first_col_width - 8, week_y + 37, "Date")
        self.canvas.drawRightString(side_margin + first_col_width - 8, week_y + 23, "Std Scheduled Hours")
        self.canvas.drawRightString(side_margin + first_col_width - 8, week_y + 7, "Overtime Hours")


        self.canvas.setFont("Helvetica", 10)
        weekday = 0
        for day,hours,part_of_invoice,holiday in days:
            if part_of_invoice:
                if day.weekday() > 4:
                    self.canvas.setFillColorRGB(0,0,0)
                else:
                    if holiday:
                        self.canvas.setFillColorRGB(0.7,0.7,0.7)
                    else:
                        self.canvas.setFillColorRGB(1,1,1)
            else:
                self.canvas.setFillColorRGB(0.7,0.7,0.7)
            self.canvas.rect(side_margin + first_col_width + weekday * day_col_width, week_y, day_col_width, week_height / 3, fill=1)
            self.canvas.rect(side_margin + first_col_width + weekday * day_col_width, week_y + week_height / 3, day_col_width, week_height / 3, fill=1)
            self.canvas.rect(side_margin + first_col_width + weekday * day_col_width, week_y + week_height / 3 * 2, day_col_width, week_height / 3, fill=1)

            if part_of_invoice and day.weekday() > 4:
                self.canvas.setFillColorRGB(1,1,1)
            else:
                self.canvas.setFillColorRGB(0,0,0)
            if part_of_invoice:
                self.canvas.drawCentredString(side_margin + first_col_width + (0.5 + weekday) * day_col_width, week_y + 5, str(0))
                self.canvas.drawCentredString(side_margin + first_col_width + (0.5 + weekday) * day_col_width, week_y + week_height / 3 + 5, str(hours))
            self.canvas.drawCentredString(side_margin + first_col_width + (0.5 + weekday) * day_col_width, week_y + 2 * week_height / 3 + 5, day.strftime('%d/%m'))
            weekday += 1

        self.week_added += 1

    def addSignatureBoxes(self, consultant, manager):
        side_margin = 40
        middle_space = 40
        down_margin = 60
        box_header_height = 25
        box_height = 100
        box_width = ( self.width - 2 * side_margin - middle_space) / 2

        self.canvas.setFillColorRGB(0,0,0)
        self.canvas.rect(side_margin, down_margin, box_width, box_height)
        self.canvas.rect(side_margin + box_width + middle_space, down_margin, box_width, box_height)
        self.canvas.rect(side_margin, down_margin + box_height, box_width, box_header_height)
        self.canvas.rect(side_margin + box_width + middle_space, down_margin + box_height, box_width, box_header_height)
        self.canvas.drawCentredString(side_margin + box_width / 2, down_margin+ box_height + 10, consultant)
        self.canvas.drawCentredString(side_margin + middle_space + box_width * 1.5, down_margin+ box_height + 10, manager)

    def save(self):
        self.canvas.showPage()
        self.canvas.save()


if __name__ == "__main__":
    buffer = StringIO()
    holidays = [date(2010,12,3)]
    pdfCreator = TimesheetPDFCreator(buffer, holidays)
    pdfCreator.setHeader(consultant="Pierre Moermans", period="December 2010", project="Track-Mail", manager="Elodie Doncq")
    pdfCreator.addWeek(48, [
            (date(2010,11,28),0),
            (date(2010,11,29),8),
            (date(2010,11,30),8),
            (date(2010,12,1),8),
            (date(2010,12,2),8),
            (date(2010,12,3),8),
            (date(2010,12,4),0)])
    pdfCreator.addWeek(49, [
            (date(2010,12,5),0),
            (date(2010,12,6),8),
            (date(2010,12,7),8),
            (date(2010,12,8),8),
            (date(2010,12,9),8),
            (date(2010,12,10),4),
            (date(2010,12,11),0)])
    pdfCreator.addSignatureBoxes(consultant="Pierre Moermans", manager="Elodie Doncq")
    pdfCreator.save()
    pdf = buffer.getvalue()
    outputFile = open("C:/timesheet.pdf", "wb")
    outputFile.write(pdf)
    outputFile.close()
  